package cc.rengu.redp.flowable.bizrule.rule;

import com.alibaba.fastjson.JSONObject;

public abstract class AccessRule implements BusiRule {

	protected final static String RULE_PREFIX = "ACCRule"; // Access rule

    @Override
    public RuleResult checkSuitable(JSONObject validateJson) {
        return RuleResult.buildSuccessResult();
    }

    @Override
	public abstract RuleResult validateData(JSONObject validateJson);

    @Override
    public JSONObject getValidateData(JSONObject requestJson) {
		return new JSONObject();
	}

    @Override
    public abstract void dataPreHandle(JSONObject validateJson);

    @Override
    public String getRulePrefix(){
        return RULE_PREFIX;
    }

    /**
     * 检查校验规则时所用的字段是否无法获取
     * (用于规则内的必须具备的条件字段，如缺失，属于内部错误，表示规则应用的程序存在问题，用户无法解决）
     *
     * @param validateJson
     * @param elements
     * @return
     */
    protected RuleResult verifyElementIsNull(JSONObject validateJson, String... elements) {
        for (String element : elements) {
            if (validateJson.getString(element) == null) {
                return RuleResult.buildFailureResult("内部错误：无法获取校验字段" + element);
            }
        }

        return null;
    }
}
